package view;
import java.awt.Color;
import java.io.File;

import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Loader;
import com.threed.jpct.Logger;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.World;

/**
 * A simple HelloWorld using the OpenGL-renderer.
 * @author EgonOlsen
 *
 */
public class HelloWorldOGL {

	private World world;
	private FrameBuffer buffer;
	private Object3D mbook;
	
	static {

	}

	public static void main(String[] args) throws Exception {
		new HelloWorldOGL().loop();
	}

	public HelloWorldOGL() throws Exception {
		world = new World();
		world.setAmbientLight(255, 255, 255);
		
		
		 Texture spot=new Texture("textures"+File.separatorChar+"book.jpg");
	     TextureManager.getInstance().addTexture("book", spot);
	     Object3D BOOK;
	     Object3D[] objs=Loader.load3DS("models"+File.separatorChar+"book.3ds",100f);
	     if (objs.length==1) {
	        BOOK=objs[0];
	        BOOK.setTexture("book");
	        //BOOK.setCulling(Object3D.CULLING_DISABLED);
	        BOOK.setRotationMatrix(new Matrix());
	        BOOK.build();
	     }
	     else {
	        // Opps...this shouldn't happen...
	        Logger.log("Wrong file format for books!", Logger.ERROR);
	        BOOK=null;
	     }
		
		mbook = BOOK.cloneObject();
		world.addObject(mbook);

		world.getCamera().setPosition(50, -50, -5);
		world.getCamera().lookAt(mbook.getTransformedCenter());
	}

	private void loop() throws Exception {
		buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
		buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
		buffer.enableRenderer(IRenderer.RENDERER_OPENGL);

		while (!org.lwjgl.opengl.Display.isCloseRequested()) {
			mbook.rotateY(0.01f);
			buffer.clear(java.awt.Color.BLUE);
			world.renderScene(buffer);
			world.draw(buffer);
			buffer.update();
			buffer.displayGLOnly();
			Thread.sleep(10);
		}
		buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
		buffer.dispose();
		System.exit(0);
	}
}

	